package com.smart.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.smart.model.Admin;
import com.smart.util.DBUtil;

public class AdminDAO {
	public Admin loginCheck(String telephoneNum, String password) throws SQLException {
		Admin admin = null;
		String sql = "select * from admin where telephone_num=? and password=?";
		Connection conn = DBUtil.getConnection();
		PreparedStatement pSt = conn.prepareCall(sql);
		pSt.setString(1, telephoneNum);
		pSt.setString(2, password);
		ResultSet rs = pSt.executeQuery();
		if (rs.next()) {
			admin = new Admin();
			admin.setId(rs.getInt("id"));
			admin.setPassword(rs.getString("password"));
			admin.setTelephoneNum(rs.getString("telephone_num"));
			admin.setType(rs.getInt("type"));
			admin.setAdminName(rs.getString("admin_name"));
			admin.setCommunityId(rs.getInt("community_id"));
		}
		return admin;
	}

	// pageIndex为第几页，pageSize为每页多少条记录
	public List<Admin> getAdminList(int pageIndex, int pageSize, int communityId) throws SQLException {
		String sql = "select * from admin where community_id=? limit ?,? ";
		List<Admin> adminList = new ArrayList<Admin>();
		Connection conn = DBUtil.getConnection();
		PreparedStatement pSt = conn.prepareStatement(sql);
		pSt.setInt(1, communityId);
		pSt.setInt(2, (pageIndex - 1) * pageSize);
		pSt.setInt(3, pageSize);
		ResultSet rs = pSt.executeQuery();
		while (rs.next()) {
			int id = rs.getInt("id");
			// 不准备返回密码，我觉得没什么用处
			String password = "";// rs.getString("password");
			int type = rs.getInt("type");
			String telephoneNum = rs.getString("telephone_num");
			String adminName = rs.getString("admin_name");
			Admin admin = new Admin();
			admin.setId(id);
			admin.setAdminName(adminName);
			admin.setPassword(password);
			admin.setTelephoneNum(telephoneNum);
			admin.setType(type);
			admin.setCommunityId(communityId);
			adminList.add(admin);
		}
		// }
		return adminList;
	}

	// 获取总数
	public int getCount(int communityId) throws SQLException {
		int count = -1;
		String sql = "select count(*) as con from admin where community_id=?";
		Connection conn = DBUtil.getConnection();
		PreparedStatement pSt = conn.prepareStatement(sql);
		pSt.setInt(1, communityId);
		ResultSet res = pSt.executeQuery();
		if (res.next())
			count = res.getInt("con");
		return count;
	}

	// 添加管理员
	public int addAdmin(String password, int type, String telephoneNum, String adminName, int communityId)
			throws SQLException {
		String sql = "insert into admin(password,type,telephone_num,admin_name,community_id) values(?,?,?,?,?)";
		Connection conn = DBUtil.getConnection();
		PreparedStatement pSt = conn.prepareCall(sql);
		pSt.setString(1, password);
		pSt.setInt(2, type);
		pSt.setString(3, telephoneNum);
		pSt.setString(4, adminName);
		pSt.setInt(5, communityId);
		return pSt.executeUpdate();
	}

	// 删除管理员
	public int deleteAdmin(int id) throws SQLException {
		String sql = "delete from admin where id=?";
		Connection conn = DBUtil.getConnection();
		PreparedStatement pSt = conn.prepareCall(sql);
		pSt.setInt(1, id);
		return pSt.executeUpdate();
	}

	// 更新管理员信息
	public int updateAdmin(Admin admin) throws SQLException {
		String sql = "update admin set password=?,telephone_num=?,type=?,admin_name=? where id=?";
		Connection conn = DBUtil.getConnection();
		PreparedStatement pSt = conn.prepareCall(sql);
		pSt.setString(1, admin.getPassword());
		pSt.setString(2, admin.getTelephoneNum());
		pSt.setInt(3, admin.getType());
		pSt.setString(4, admin.getAdminName());
		pSt.setInt(5, admin.getId());
		return pSt.executeUpdate();
	}

	// 根据电话号码进行搜索
	public List<Admin> searchAdminByTele(String telephoneNum, int communityId) throws SQLException {
		List<Admin> adminList = new ArrayList<Admin>();
		String sql = "select * from admin where telephone_num=? and community_id=?";
		Connection conn = DBUtil.getConnection();
		PreparedStatement pSt = conn.prepareCall(sql);
		pSt.setString(1, telephoneNum);
		pSt.setInt(2, communityId);
		ResultSet rs = pSt.executeQuery();
		while (rs.next()) {
			Admin admin = new Admin();
			int id = rs.getInt("id");
			// 不准备返回密码，我觉得没什么用处
			String password = "";// rs.getString("password");
			int type = rs.getInt("type");
			String adminName = rs.getString("admin_name");
			admin.setAdminName(adminName);
			admin.setId(id);
			admin.setPassword(password);
			admin.setType(type);
			admin.setTelephoneNum(telephoneNum);
			admin.setCommunityId(communityId);
			adminList.add(admin);
		}
		return adminList;
	}

	// 根据管理员姓名进行搜索
	public List<Admin> searchAdminByAdminName(String adminName, int communityId) throws SQLException {
		List<Admin> adminList = new ArrayList<Admin>();
		String sql = "select * from admin where admin_name=? and community_id=?";
		Connection conn = DBUtil.getConnection();
		PreparedStatement pSt = conn.prepareCall(sql);
		pSt.setString(1, adminName);
		pSt.setInt(2, communityId);
		ResultSet rs = pSt.executeQuery();
		while (rs.next()) {
			Admin admin = new Admin();
			int id = rs.getInt("id");
			// 不准备返回密码，我觉得没什么用处
			String password = "";// rs.getString("password");
			int type = rs.getInt("type");
			String telephoneNum = rs.getString("telephone_num");
			admin.setAdminName(adminName);
			admin.setId(id);
			admin.setPassword(password);
			admin.setType(type);
			admin.setTelephoneNum(telephoneNum);
			admin.setCommunityId(communityId);
			adminList.add(admin);
		}
		return adminList;
	}
}
